<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>中俊为 | we harmonyos</title>
    <meta name="generator" content="VuePress 1.9.7">
    <script>
          var _hmt = _hmt || [];
          (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?39485dba914ae63b24732b6a90688f0b";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
          })();
      </script>
    <link rel="manifest" href="/manifest.webmanifest" crossorigin="use-credentials">
    <meta name="description" content="weharmonyos 鸿蒙论坛 鸿蒙注释 开源鸿蒙 鸿蒙源码 openharmony源码 HarmonyOS源码 HarmonyOS HarmonyOS生态 openharmony开发者文档 鸿蒙生态 鸿蒙系统 华为操作系统 鸿蒙研究站 开放原子开源基金会 百万汉字注解鸿蒙 百篇博客分析鸿蒙">
    <meta property="og:url" content="/blog/25.html">
    <meta property="og:site_name" content="中俊为 | we harmonyos">
    <meta property="og:description" content="本篇关键词：、、、 下载 &gt;&gt; 离线文档.鸿蒙内核源码分析(百篇博客分析.挖透鸿蒙内核).pdf.zip) 任务管理相关篇为: v21.07 鸿蒙内核源码分析(任务控制块) | 内核最重要的概念; v22.05 鸿蒙内核源码分析(并发并行) | 如何搞清楚它俩区分; v23.03 鸿蒙内核源码分析(就绪队列) | 美好的事物永远值得等待; v24.0">
    <meta property="og:type" content="article">
    <meta property="og:locale" content="en-US">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:image:alt" content="中俊为 | we harmonyos">
    <meta name="theme-color" content="#46bd87">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    
    <link rel="preload" href="/assets/css/0.styles.087eb7d3.css" as="style"><link rel="preload" href="/assets/js/app.9a8b95d8.js" as="script"><link rel="preload" href="/assets/js/vendors~layout-Layout.c1e797d4.js" as="script"><link rel="preload" href="/assets/js/vendors~layout-Blog~layout-Layout~layout-NotFound.f75cf258.js" as="script"><link rel="preload" href="/assets/js/page--10bd3330.b5165172.js" as="script"><link rel="prefetch" href="/assets/js/160.a8046d43.js"><link rel="prefetch" href="/assets/js/161.29f8564f.js"><link rel="prefetch" href="/assets/js/162.249ee491.js"><link rel="prefetch" href="/assets/js/163.c57d1577.js"><link rel="prefetch" href="/assets/js/164.7a788aaa.js"><link rel="prefetch" href="/assets/js/165.c333afb2.js"><link rel="prefetch" href="/assets/js/166.9bcb7cd6.js"><link rel="prefetch" href="/assets/js/layout-Blog.4a9781d1.js"><link rel="prefetch" href="/assets/js/layout-Layout.7e277e0b.js"><link rel="prefetch" href="/assets/js/layout-NotFound.9326b14e.js"><link rel="prefetch" href="/assets/js/layout-Slide.8dd861e4.js"><link rel="prefetch" href="/assets/js/page--00854288.e2bc8e8a.js"><link rel="prefetch" href="/assets/js/page--00f8db0a.b2a35fe6.js"><link rel="prefetch" href="/assets/js/page--03ad6df0.d48f4c19.js"><link rel="prefetch" href="/assets/js/page--04b12ba8.e8add78d.js"><link rel="prefetch" href="/assets/js/page--070d2528.d24cd503.js"><link rel="prefetch" href="/assets/js/page--086560f0.a6be2844.js"><link rel="prefetch" href="/assets/js/page--090ad32d.fc7da3c3.js"><link rel="prefetch" href="/assets/js/page--0c054030.2886024e.js"><link rel="prefetch" href="/assets/js/page--0d9507c8.a27dee03.js"><link rel="prefetch" href="/assets/js/page--1233b0b2.b5caca9c.js"><link rel="prefetch" href="/assets/js/page--13650e88.61330a2b.js"><link rel="prefetch" href="/assets/js/page--14c850b1.d9a40c31.js"><link rel="prefetch" href="/assets/js/page--15752630.25f9c756.js"><link rel="prefetch" href="/assets/js/page--19150570.58df8995.js"><link rel="prefetch" href="/assets/js/page--19ecf128.130d012b.js"><link rel="prefetch" href="/assets/js/page--1c48eaa8.9a689e30.js"><link rel="prefetch" href="/assets/js/page--1d4dfe96.bd551921.js"><link rel="prefetch" href="/assets/js/page--1dccf870.03d7d9cc.js"><link rel="prefetch" href="/assets/js/page--2074d3c8.06bf5706.js"><link rel="prefetch" href="/assets/js/page--22213c96.e703257a.js"><link rel="prefetch" href="/assets/js/page--2284eb70.514124de.js"><link rel="prefetch" href="/assets/js/page--22d0cd48.11ea1522.js"><link rel="prefetch" href="/assets/js/page--252cc6c8.f9acae83.js"><link rel="prefetch" href="/assets/js/page--2624cab0.4535fea2.js"><link rel="prefetch" href="/assets/js/page--269ff8f9.012e8ae6.js"><link rel="prefetch" href="/assets/js/page--26fcb668.35b2da5d.js"><link rel="prefetch" href="/assets/js/page--2958afe8.216913bc.js"><link rel="prefetch" href="/assets/js/page--2adcbdb0.6dcb4c27.js"><link rel="prefetch" href="/assets/js/page--2bb4a968.aac31797.js"><link rel="prefetch" href="/assets/js/page--2d849908.7bb4e817.js"><link rel="prefetch" href="/assets/js/page--2e10a2e8.42bef48d.js"><link rel="prefetch" href="/assets/js/page--2f94b0b0.0c993df5.js"><link rel="prefetch" href="/assets/js/page--2fe09288.77769dcb.js"><link rel="prefetch" href="/assets/js/page--323c8c08.254a5f78.js"><link rel="prefetch" href="/assets/js/page--33348ff0.a5f99eb0.js"><link rel="prefetch" href="/assets/js/page--340c7ba8.2c9570cb.js"><link rel="prefetch" href="/assets/js/page--34988588.8c8d0363.js"><link rel="prefetch" href="/assets/js/page--36687528.67d84e0d.js"><link rel="prefetch" href="/assets/js/page--37d6dfd6.88e9cd42.js"><link rel="prefetch" href="/assets/js/page--37ec82f0.5419c82d.js"><link rel="prefetch" href="/assets/js/page--37f7cf15.65f72e55.js"><link rel="prefetch" href="/assets/js/page--38c46ea8.5d5150f9.js"><link rel="prefetch" href="/assets/js/page--3a945e48.bf9758fb.js"><link rel="prefetch" href="/assets/js/page--3ab2204c.e08d6316.js"><link rel="prefetch" href="/assets/js/page--3b206828.d83e3926.js"><link rel="prefetch" href="/assets/js/page--3ca475f0.49e5acce.js"><link rel="prefetch" href="/assets/js/page--3cf057c8.8364331d.js"><link rel="prefetch" href="/assets/js/page--3f4c5148.587a8360.js"><link rel="prefetch" href="/assets/js/page--40445530.486bb516.js"><link rel="prefetch" href="/assets/js/page--411c40e8.2eedba0e.js"><link rel="prefetch" href="/assets/js/page--41a84ac8.f4a360a3.js"><link rel="prefetch" href="/assets/js/page--4220f8b5.19b4371f.js"><link rel="prefetch" href="/assets/js/page--43783a68.4faf0866.js"><link rel="prefetch" href="/assets/js/page--44fc4830.a160662c.js"><link rel="prefetch" href="/assets/js/page--45d433e8.560fef87.js"><link rel="prefetch" href="/assets/js/page--47a42388.052314b4.js"><link rel="prefetch" href="/assets/js/page--47d07816.66731286.js"><link rel="prefetch" href="/assets/js/page--48302d68.8d6493cf.js"><link rel="prefetch" href="/assets/js/page--49b43b30.e3684736.js"><link rel="prefetch" href="/assets/js/page--4a001d08.47f7ec45.js"><link rel="prefetch" href="/assets/js/page--4c5c1688.b7ec1f32.js"><link rel="prefetch" href="/assets/js/page--4d541a70.ee807705.js"><link rel="prefetch" href="/assets/js/page--4e2c0628.4f7105a4.js"><link rel="prefetch" href="/assets/js/page--4eb81008.725d26fe.js"><link rel="prefetch" href="/assets/js/page--5087ffa8.dac2aae0.js"><link rel="prefetch" href="/assets/js/page--520c0d70.2beedefb.js"><link rel="prefetch" href="/assets/js/page--52e3f928.2645087c.js"><link rel="prefetch" href="/assets/js/page--53cd0b44.96d4a285.js"><link rel="prefetch" href="/assets/js/page--54eb2c5e.27eb702b.js"><link rel="prefetch" href="/assets/js/page--553ff2a8.fba8ee4f.js"><link rel="prefetch" href="/assets/js/page--5686ab01.547b633c.js"><link rel="prefetch" href="/assets/js/page--56c40070.4e35708a.js"><link rel="prefetch" href="/assets/js/page--570fe248.2b301b36.js"><link rel="prefetch" href="/assets/js/page--596bdbc8.c74cd724.js"><link rel="prefetch" href="/assets/js/page--5a63dfb0.bbd49a9b.js"><link rel="prefetch" href="/assets/js/page--5bc7d548.eda26865.js"><link rel="prefetch" href="/assets/js/page--5f1bd2b0.e02cf88d.js"><link rel="prefetch" href="/assets/js/page--5ff3be68.b2b0e76e.js"><link rel="prefetch" href="/assets/js/page--624fb7e8.666fdc84.js"><link rel="prefetch" href="/assets/js/page--63d3c5b0.b587d38f.js"><link rel="prefetch" href="/assets/js/page--68d79a88.86f62e4d.js"><link rel="prefetch" href="/assets/js/page--6c2b97f0.ed46abea.js"><link rel="prefetch" href="/assets/js/page--6cc6a596.b3827451.js"><link rel="prefetch" href="/assets/js/page--6ea7a148.61a9bc10.js"><link rel="prefetch" href="/assets/js/page--70e38af0.eb8ffc53.js"><link rel="prefetch" href="/assets/js/page--7180d470.73a3a20e.js"><link rel="prefetch" href="/assets/js/page--752f83e8.d5c31640.js"><link rel="prefetch" href="/assets/js/page--778b7d68.5e8e5ea9.js"><link rel="prefetch" href="/assets/js/page--7bb76688.53e305e3.js"><link rel="prefetch" href="/assets/js/page--7c59a86a.ecf88c30.js"><link rel="prefetch" href="/assets/js/page--7df35030.cfa388f4.js"><link rel="prefetch" href="/assets/js/page--7e136008.230e9a15.js"><link rel="prefetch" href="/assets/js/page--89935db0.f79f9e25.js"><link rel="prefetch" href="/assets/js/page--96a322f0.412d537f.js"><link rel="prefetch" href="/assets/js/page--9b5b15f0.a5231547.js"><link rel="prefetch" href="/assets/js/page--a3b2e830.f4ba68fa.js"><link rel="prefetch" href="/assets/js/page--a86adb30.ec08486b.js"><link rel="prefetch" href="/assets/js/page--ad22ce30.4872eca4.js"><link rel="prefetch" href="/assets/js/page--b0c2ad70.8637057e.js"><link rel="prefetch" href="/assets/js/page--b57aa070.43215f9e.js"><link rel="prefetch" href="/assets/js/page--ba329370.b5f22d13.js"><link rel="prefetch" href="/assets/js/page--bdd272b0.aff1d1a9.js"><link rel="prefetch" href="/assets/js/page--c28a65b0.978514c4.js"><link rel="prefetch" href="/assets/js/page--c606edca.49d3396e.js"><link rel="prefetch" href="/assets/js/page--c74258b0.f85195bc.js"><link rel="prefetch" href="/assets/js/page--cae237f0.8a976cc1.js"><link rel="prefetch" href="/assets/js/page--cf9a2af0.7ad20ea2.js"><link rel="prefetch" href="/assets/js/page--d4521df0.a0a74934.js"><link rel="prefetch" href="/assets/js/page--d7f1fd30.160f138a.js"><link rel="prefetch" href="/assets/js/page--dca9f030.9127ab28.js"><link rel="prefetch" href="/assets/js/page--e161e330.34876a6f.js"><link rel="prefetch" href="/assets/js/page--e4f35ef4.690d3ccb.js"><link rel="prefetch" href="/assets/js/page--e501c270.8dc088ee.js"><link rel="prefetch" href="/assets/js/page--e9b9b570.b94764ed.js"><link rel="prefetch" href="/assets/js/page--ea4e0a70.94492571.js"><link rel="prefetch" href="/assets/js/page--ee71a870.b9944792.js"><link rel="prefetch" href="/assets/js/page--f21187b0.d00a5788.js"><link rel="prefetch" href="/assets/js/page--f6c97ab0.ecb91d64.js"><link rel="prefetch" href="/assets/js/page--fb816db0.c1f07c37.js"><link rel="prefetch" href="/assets/js/page--fcf2f51e.71000d71.js"><link rel="prefetch" href="/assets/js/page--ff214cf0.aaa8b0fc.js"><link rel="prefetch" href="/assets/js/page-GN.2f20b55f.js"><link rel="prefetch" href="/assets/js/page-GNFrequentlyAskedQuestions.24e61c26.js"><link rel="prefetch" href="/assets/js/page-GNLanguageandOperation.35e22ff2.js"><link rel="prefetch" href="/assets/js/page-GNQuickStartguide.7cd8e521.js"><link rel="prefetch" href="/assets/js/page-GNReference.1d5d0c6c.js"><link rel="prefetch" href="/assets/js/page-GNSimpleBuildExample.2eb0b7eb.js"><link rel="prefetch" href="/assets/js/page-GNStyleGuide.8a407135.js"><link rel="prefetch" href="/assets/js/page-GNU汇编教程.d208e4d7.js"><link rel="prefetch" href="/assets/js/page-GNvimsyntaxplugin.58d1459e.js"><link rel="prefetch" href="/assets/js/page-GN快速入门.b708bb6a.js"><link rel="prefetch" href="/assets/js/page-Home.97282d71.js"><link rel="prefetch" href="/assets/js/page-HowGNhandlescross-compiling.6ca64957.js"><link rel="prefetch" href="/assets/js/page-Introduction.292088e0.js"><link rel="prefetch" href="/assets/js/page-ninja快速入门.92729e64.js"><link rel="prefetch" href="/assets/js/page-《孙子兵法》全文.402792c5.js"><link rel="prefetch" href="/assets/js/page-《论语》全文.85da0da0.js"><link rel="prefetch" href="/assets/js/page-《诗经》全文.99ebeb52.js"><link rel="prefetch" href="/assets/js/page-《道德经》全文.43627e4e.js"><link rel="prefetch" href="/assets/js/page-两个文档.eb8dd1b7.js"><link rel="prefetch" href="/assets/js/page-主流站点覆盖定期同步更新.1ad02178.js"><link rel="prefetch" href="/assets/js/page-手机版电脑版.b5e8dfea.js"><link rel="prefetch" href="/assets/js/page-捐助方式.6ca91d44.js"><link rel="prefetch" href="/assets/js/page-来源LiteOS和LiteOS-A内核图解.8fa16705.js"><link rel="prefetch" href="/assets/js/page-电脑版手机版.67a0b4a4.js"><link rel="prefetch" href="/assets/js/page-编程语言相关书籍.d9adf7cb.js"><link rel="prefetch" href="/assets/js/page-鸿蒙内核源码分析.bbb451c7.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.46b2de48.js"><link rel="prefetch" href="/assets/js/vendors~mermaid.627fb4be.js"><link rel="prefetch" href="/assets/js/vendors~photo-swipe.cbde97f9.js"><link rel="prefetch" href="/assets/js/vendors~reveal.77b21b7e.js">
    <link rel="stylesheet" href="/assets/css/0.styles.087eb7d3.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container has-navbar has-sidebar has-anchor"><header class="navbar"><!----> <div class="content__navbar-start"></div> <button title="Sidebar Button" class="sidebar-button"><span class="icon"></span></button> <a href="/" class="home-link router-link-active"><!----> <!----> <span class="site-name can-hide">中俊为 | we harmonyos</span></a> <!----> <div class="content__navbar-center"></div> <div class="links"><button tabindex="-1" aria-hidden="true" class="color-button"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="skin-icon"><path d="M224 800c0 9.6 3.2 44.8 6.4 54.4 6.4 48-48 76.8-48 76.8s80 41.6 147.2 0 134.4-134.4
        38.4-195.2c-22.4-12.8-41.6-19.2-57.6-19.2C259.2 716.8 227.2 761.6 224 800zM560 675.2l-32
        51.2c-51.2 51.2-83.2 32-83.2 32 25.6 67.2 0 112-12.8 128 25.6 6.4 51.2 9.6 80 9.6 54.4 0
        102.4-9.6 150.4-32l0 0c3.2 0 3.2-3.2 3.2-3.2 22.4-16 12.8-35.2
        6.4-44.8-9.6-12.8-12.8-25.6-12.8-41.6 0-54.4 60.8-99.2 137.6-99.2 6.4 0 12.8 0 22.4
        0 12.8 0 38.4 9.6 48-25.6 0-3.2 0-3.2 3.2-6.4 0-3.2 3.2-6.4 3.2-6.4 6.4-16 6.4-16 6.4-19.2
        9.6-35.2 16-73.6 16-115.2 0-105.6-41.6-198.4-108.8-268.8C704 396.8 560 675.2 560 675.2zM224
        419.2c0-28.8 22.4-51.2 51.2-51.2 28.8 0 51.2 22.4 51.2 51.2 0 28.8-22.4 51.2-51.2 51.2C246.4
        470.4 224 448 224 419.2zM320 284.8c0-22.4 19.2-41.6 41.6-41.6 22.4 0 41.6 19.2 41.6 41.6 0
        22.4-19.2 41.6-41.6 41.6C339.2 326.4 320 307.2 320 284.8zM457.6 208c0-12.8 12.8-25.6 25.6-25.6
        12.8 0 25.6 12.8 25.6 25.6 0 12.8-12.8 25.6-25.6 25.6C470.4 233.6 457.6 220.8 457.6 208zM128
        505.6C128 592 153.6 672 201.6 736c28.8-60.8 112-60.8 124.8-60.8-16-51.2 16-99.2
        16-99.2l316.8-422.4c-48-19.2-99.2-32-150.4-32C297.6 118.4 128 291.2 128 505.6zM764.8
        86.4c-22.4 19.2-390.4 518.4-390.4 518.4-22.4 28.8-12.8 76.8 22.4 99.2l9.6 6.4c35.2 22.4
        80 12.8 99.2-25.6 0 0 6.4-12.8 9.6-19.2 54.4-105.6 275.2-524.8 288-553.6
        6.4-19.2-3.2-32-19.2-32C777.6 76.8 771.2 80 764.8 86.4z"></path></svg> <div class="color-picker-menu" style="display:none;"><div class="theme-options"><ul class="themecolor-select"><label for="themecolor-select">Theme Color:</label> <li><span class="default-theme"></span></li> </ul> <div class="darkmode-toggle"><label for="darkmode-toggle" class="desc">Theme Mode:</label> <div class="darkmode-switch"><div class="item day"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="icon light-icon"><path d="M512 256a42.667 42.667 0 0 0 42.667-42.667V128a42.667 42.667 0 0 0-85.334 0v85.333A42.667 42.667 0 0 0 512 256zm384 213.333h-85.333a42.667 42.667 0 0 0 0 85.334H896a42.667 42.667 0 0 0 0-85.334zM256 512a42.667 42.667 0 0 0-42.667-42.667H128a42.667 42.667 0 0 0 0 85.334h85.333A42.667 42.667 0 0 0 256 512zm9.387-298.667a42.667 42.667 0 0 0-59.307 62.72l61.44 59.307a42.667 42.667 0 0 0 31.147 11.947 42.667 42.667 0 0 0 30.72-13.227 42.667 42.667 0 0 0 0-60.16zm459.946 133.974a42.667 42.667 0 0 0 29.44-11.947l61.44-59.307a42.667 42.667 0 0 0-57.6-62.72l-61.44 60.587a42.667 42.667 0 0 0 0 60.16 42.667 42.667 0 0 0 28.16 13.227zM512 768a42.667 42.667 0 0 0-42.667 42.667V896a42.667 42.667 0 0 0 85.334 0v-85.333A42.667 42.667 0 0 0 512 768zm244.48-79.36a42.667 42.667 0 0 0-59.307 61.44l61.44 60.587a42.667 42.667 0 0 0 29.44 11.946 42.667 42.667 0 0 0 30.72-12.8 42.667 42.667 0 0 0 0-60.586zm-488.96 0-61.44 59.307a42.667 42.667 0 0 0 0 60.586 42.667 42.667 0 0 0 30.72 12.8 42.667 42.667 0 0 0 28.587-10.666l61.44-59.307a42.667 42.667 0 0 0-59.307-61.44zM512 341.333A170.667 170.667 0 1 0 682.667 512 170.667 170.667 0 0 0 512 341.333z" fill="currentColor"></path></svg></div> <div class="item auto active"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="icon auto-icon"><path d="M460.864 539.072H564.8L510.592 376l-49.728 163.072zM872 362.368V149.504H659.648L510.528 0l-149.12 149.504H149.12v212.928L0 511.872l149.12 149.504v212.928h212.352l149.12 149.504 149.12-149.504h212.352V661.376l149.12-149.504L872 362.368zM614.464 693.12l-31.616-90.624H438.272l-31.616 90.624h-85.888l144.576-407.68h90.368l144.576 407.68h-85.824zm0 0" fill="currentColor"></path></svg></div> <div class="item night"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="icon dark-icon"><path d="M935.539 630.402c-11.43-11.432-28.674-14.739-43.531-8.354-46.734 20.103-96.363 30.297-147.508 30.297-99.59 0-193.221-38.784-263.64-109.203-108.637-108.637-139.61-270.022-78.908-411.148a39.497 39.497 0 0 0-51.886-51.887c-52.637 22.64-100.017 54.81-140.826 95.616-85.346 85.346-132.346 198.821-132.346 319.52 0 120.7 47.001 234.172 132.347 319.519S408.063 947.11 528.76 947.11c120.7 0 234.172-47.003 319.52-132.351 40.809-40.81 72.978-88.19 95.616-140.826a39.497 39.497 0 0 0-8.356-43.532z" fill="currentColor"></path></svg></div></div> <!----></div></div></div></button> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link router-link-active"><!---->
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档中心" class="dropdown-title"><span class="title"><!---->
        文档中心
      </span> <span class="arrow"></span></button> <ul class="nav-dropdown"><li class="dropdown-item"><a href="http://open.weharmonyos.com/zh-cn/readme/Readme-CN.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  子系统
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><a href="http://open.weharmonyos.com/zh-cn/device-dev/website.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  南向
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><a href="http://open.weharmonyos.com/zh-cn/application-dev/website.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  北向
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="博客中心" class="dropdown-title"><span class="title"><!---->
        博客中心
      </span> <span class="arrow"></span></button> <ul class="nav-dropdown"><li class="dropdown-item"><a href="/blog/101.html" class="nav-link"><!---->
  总目录
</a></li><li class="dropdown-item"><a href="/blog/index/1_base.html" class="nav-link"><!---->
  基础知识
</a></li><li class="dropdown-item"><a href="/blog/index/2_process.html" class="nav-link"><!---->
  进程管理
</a></li><li class="dropdown-item"><a href="/blog/index/3_task.html" class="nav-link"><!---->
  任务管理
</a></li><li class="dropdown-item"><a href="/blog/index/4_mem.html" class="nav-link"><!---->
  内存管理
</a></li><li class="dropdown-item"><a href="/blog/index/5_ipc.html" class="nav-link"><!---->
  进程通讯
</a></li><li class="dropdown-item"><a href="/blog/index/6_fs.html" class="nav-link"><!---->
  文件系统
</a></li><li class="dropdown-item"><a href="/blog/index/7_hw.html" class="nav-link"><!---->
  软硬接口
</a></li><li class="dropdown-item"><a href="/blog/index/8_asm.html" class="nav-link"><!---->
  内核汇编
</a></li><li class="dropdown-item"><a href="/blog/index/9_run.html" class="nav-link"><!---->
  编译运行
</a></li><li class="dropdown-item"><a href="/blog/index/10_test.html" class="nav-link"><!---->
  调测工具
</a></li><li class="dropdown-item"><a href="/blog/index/0_so.html" class="nav-link"><!---->
  前因后果
</a></li><li class="dropdown-item"><a href="/extsite.html" class="nav-link"><!---->
  前往 &gt;&gt; 外站
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="论坛中心" class="dropdown-title"><span class="title"><!---->
        论坛中心
      </span> <span class="arrow"></span></button> <ul class="nav-dropdown"><li class="dropdown-item"><a href="http://bbs.weharmonyos.com" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  论坛首页
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><a href="http://bbs.weharmonyos.com/forum-42-1.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  问题建议
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="手册中心" class="dropdown-title"><span class="title"><!---->
        手册中心
      </span> <span class="arrow"></span></button> <ul class="nav-dropdown"><li class="dropdown-item"><a href="http://doxygen.weharmonyos.com/index.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  内核参考手册
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><a href="/compile/assembly.html" class="nav-link"><!---->
  gnu 汇编教程
</a></li><li class="dropdown-item"><a href="/compile/gn/docs/" class="nav-link"><!---->
  gn 参考手册
</a></li><li class="dropdown-item"><a href="/compile/ninja/Readme-CN.html" class="nav-link"><!---->
  ninja 参考手册
</a></li><li class="dropdown-item"><a href="/compile/fhs.html" class="nav-link"><!---->
  fhs 规范
</a></li></ul></div></div><div class="nav-item"><a href="/donate.html" class="nav-link"><!---->
  赞助中心
</a></div></nav> <!----> <!----> <!----> <div class="content__navbar-end"></div></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><!----> <!----> <div class="content__sidebar-top"></div> <nav class="sidebar-nav-links"><div class="nav-item"><a href="/" class="nav-link router-link-active"><!---->
  首页
</a></div><div class="nav-item"><div class="mobile-dropdown-wrapper"><button type="button" aria-label="文档中心" class="dropdown-title"><span class="title"><!---->
      文档中心
    </span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://open.weharmonyos.com/zh-cn/readme/Readme-CN.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  子系统
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://open.weharmonyos.com/zh-cn/device-dev/website.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  南向
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://open.weharmonyos.com/zh-cn/application-dev/website.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  北向
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="mobile-dropdown-wrapper"><button type="button" aria-label="博客中心" class="dropdown-title"><span class="title"><!---->
      博客中心
    </span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/101.html" class="nav-link"><!---->
  总目录
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/1_base.html" class="nav-link"><!---->
  基础知识
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/2_process.html" class="nav-link"><!---->
  进程管理
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/3_task.html" class="nav-link"><!---->
  任务管理
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/4_mem.html" class="nav-link"><!---->
  内存管理
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/5_ipc.html" class="nav-link"><!---->
  进程通讯
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/6_fs.html" class="nav-link"><!---->
  文件系统
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/7_hw.html" class="nav-link"><!---->
  软硬接口
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/8_asm.html" class="nav-link"><!---->
  内核汇编
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/9_run.html" class="nav-link"><!---->
  编译运行
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/10_test.html" class="nav-link"><!---->
  调测工具
</a></li><li class="dropdown-item"><!----> <a href="/blog/index/0_so.html" class="nav-link"><!---->
  前因后果
</a></li><li class="dropdown-item"><!----> <a href="/extsite.html" class="nav-link"><!---->
  前往 &gt;&gt; 外站
</a></li></ul></div></div><div class="nav-item"><div class="mobile-dropdown-wrapper"><button type="button" aria-label="论坛中心" class="dropdown-title"><span class="title"><!---->
      论坛中心
    </span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://bbs.weharmonyos.com" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  论坛首页
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://bbs.weharmonyos.com/forum-42-1.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  问题建议
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="mobile-dropdown-wrapper"><button type="button" aria-label="手册中心" class="dropdown-title"><span class="title"><!---->
      手册中心
    </span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://doxygen.weharmonyos.com/index.html" target="_blank" rel="noopener noreferrer" class="nav-link external"><!---->
  内核参考手册
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="/compile/assembly.html" class="nav-link"><!---->
  gnu 汇编教程
</a></li><li class="dropdown-item"><!----> <a href="/compile/gn/docs/" class="nav-link"><!---->
  gn 参考手册
</a></li><li class="dropdown-item"><!----> <a href="/compile/ninja/Readme-CN.html" class="nav-link"><!---->
  ninja 参考手册
</a></li><li class="dropdown-item"><!----> <a href="/compile/fhs.html" class="nav-link"><!---->
  fhs 规范
</a></li></ul></div></div><div class="nav-item"><a href="/donate.html" class="nav-link"><!---->
  赞助中心
</a></div> <!----></nav> <!----> <div class="content__sidebar-center"></div> <ul class="sidebar-links"><li><a href="/blog/101.html" class="sidebar-link">总目录</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">基础知识 | 10</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">进程管理 | 10</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable open"><!----> <span class="title">任务管理 | 10</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/index/3_task.html" class="sidebar-link">本章目录</a></li><li><a href="/blog/21.html" class="sidebar-link">任务控制块</a></li><li><a href="/blog/22.html" class="sidebar-link">并发并行</a></li><li><a href="/blog/23.html" class="sidebar-link">就绪队列</a></li><li><a href="/blog/24.html" class="sidebar-link">调度机制</a></li><li><a href="/blog/25.html" aria-current="page" class="active sidebar-link">任务管理</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/26.html" class="sidebar-link">用栈方式</a></li><li><a href="/blog/27.html" class="sidebar-link">软件定时器</a></li><li><a href="/blog/28.html" class="sidebar-link">控制台</a></li><li><a href="/blog/29.html" class="sidebar-link">远程登录</a></li><li><a href="/blog/30.html" class="sidebar-link">协议栈</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">内存管理 | 10</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">通讯机制 | 14</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">文件系统 | 10</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">硬件架构 | 9</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">内核汇编 | 10</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">编译运行 | 13</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">调测工具 | 4</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading clickable"><!----> <span class="title">前因后果 | 4</span> <span class="arrow right"></span></p> <!----></section></li></ul> <!----> <div class="content__sidebar-bottom"></div> <!----></aside> <main class="page"><nav class="breadcrumb disable"><!----></nav> <!----> <div class="content__page-top"></div> <div vocab="https://schema.org/" typeof="Article" class="page-title"><h1><!----> <span property="headline"></span></h1> <div class="page-info"><!----> </div> <!----> <hr></div> <div class="anchor-place-holder"><aside id="anchor"><div class="anchor-wrapper"><ul class="anchor-list"><li class="anchor"><a href="/blog/25.html#任务即线程" class="anchor-link heading3"><div>任务即线程</div></a></li><li class="anchor"><a href="/blog/25.html#官方是怎么描述线程的" class="anchor-link heading3"><div>官方是怎么描述线程的</div></a></li><li class="anchor"><a href="/blog/25.html#执行task命令" class="anchor-link heading3"><div>执行task命令</div></a></li><li class="anchor"><a href="/blog/25.html#task长得什么样子" class="anchor-link heading3"><div>task长得什么样子</div></a></li><li class="anchor"><a href="/blog/25.html#task怎么管理" class="anchor-link heading3"><div>Task怎么管理</div></a></li><li class="anchor"><a href="/blog/25.html#什么是任务池" class="anchor-link heading3"><div>什么是任务池？</div></a></li><li class="anchor"><a href="/blog/25.html#就绪队列是怎么回事" class="anchor-link heading3"><div>就绪队列是怎么回事</div></a></li><li class="anchor"><a href="/blog/25.html#任务栈是怎么回事" class="anchor-link heading3"><div>任务栈是怎么回事</div></a></li><li class="anchor"><a href="/blog/25.html#任务栈初始化" class="anchor-link heading3"><div>任务栈初始化</div></a></li><li class="anchor"><a href="/blog/25.html#task函数集" class="anchor-link heading3"><div>Task函数集</div></a></li><li class="anchor"><a href="/blog/25.html#使用场景和功能" class="anchor-link heading3"><div>使用场景和功能</div></a></li><li class="anchor"><a href="/blog/25.html#创建任务的过程" class="anchor-link heading3"><div>创建任务的过程</div></a></li><li class="anchor"><a href="/blog/25.html#百文说内核-抓住主脉络" class="anchor-link heading3"><div>百文说内核 | 抓住主脉络</div></a></li><li class="anchor"><a href="/blog/25.html#百万注源码-处处扣细节" class="anchor-link heading3"><div>百万注源码 | 处处扣细节</div></a></li><li class="anchor"><a href="/blog/25.html#关注不迷路-代码即人生" class="anchor-link heading3"><div>关注不迷路 | 代码即人生</div></a></li></ul></div></aside></div> <!----> <div class="content__content-top"></div> <div class="theme-default-content content__default"><p>本篇关键词：、、、</p> <p><a href="https://weharmony.gitee.io/blog/25.html" target="_blank" rel="noopener noreferrer"><img src="https://weharmonyos.oss-cn-hangzhou.aliyuncs.com/resources/index/25.png" alt=""></a></p> <p><a href="https://weharmonyos.oss-cn-hangzhou.aliyuncs.com/resources/pdf/%E9%B8%BF%E8%92%99%E5%86%85%E6%A0%B8%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(%E7%99%BE%E7%AF%87%E5%8D%9A%E5%AE%A2%E5%88%86%E6%9E%90.%E6%8C%96%E9%80%8F%E9%B8%BF%E8%92%99%E5%86%85%E6%A0%B8).zip" target="_blank" rel="noopener noreferrer">下载 &gt;&gt; 离线文档.鸿蒙内核源码分析(百篇博客分析.挖透鸿蒙内核).pdf</a></p> <p>任务管理相关篇为:</p> <ul><li><a href="http://weharmonyos.com/blog/21.html" target="_blank" rel="noopener noreferrer">v21.07 鸿蒙内核源码分析(任务控制块) | 内核最重要的概念</a></li> <li><a href="http://weharmonyos.com/blog/22.html" target="_blank" rel="noopener noreferrer">v22.05 鸿蒙内核源码分析(并发并行) | 如何搞清楚它俩区分</a></li> <li><a href="http://weharmonyos.com/blog/23.html" target="_blank" rel="noopener noreferrer">v23.03 鸿蒙内核源码分析(就绪队列) | 美好的事物永远值得等待</a></li> <li><a href="http://weharmonyos.com/blog/24.html" target="_blank" rel="noopener noreferrer">v24.08 鸿蒙内核源码分析(调度机制) | 公平是相对的</a></li> <li><a href="http://weharmonyos.com/blog/25.html" target="_blank" rel="noopener noreferrer">v25.05 鸿蒙内核源码分析(任务管理) | 如何管理任务池</a></li> <li><a href="http://weharmonyos.com/blog/26.html" target="_blank" rel="noopener noreferrer">v26.03 鸿蒙内核源码分析(用栈方式) | 谁来提供程序运行场地</a></li> <li><a href="http://weharmonyos.com/blog/27.html" target="_blank" rel="noopener noreferrer">v27.02 鸿蒙内核源码分析(软件定时器) | 内核最高级任务竟是它</a></li> <li><a href="http://weharmonyos.com/blog/28.html" target="_blank" rel="noopener noreferrer">v28.01 鸿蒙内核源码分析(控制台) | 一个让很多人模糊的概念</a></li> <li><a href="http://weharmonyos.com/blog/29.html" target="_blank" rel="noopener noreferrer">v29.01 鸿蒙内核源码分析(远程登录) | 内核如何接待远方的客人</a></li> <li><a href="http://weharmonyos.com/blog/30.html" target="_blank" rel="noopener noreferrer">v30.01 鸿蒙内核源码分析(协议栈) | 正在制作中 ... </a></li></ul> <h3 id="任务即线程"><strong>任务即线程</strong></h3> <p>在鸿蒙内核中，广义上可理解为一个任务就是一个线程</p> <h3 id="官方是怎么描述线程的"><strong>官方是怎么描述线程的</strong></h3> <p>基本概念<br>
从系统的角度看，线程是竞争系统资源的最小运行单元。线程可以使用或等待CPU、使用内存空间等系统资源，并独立于其它线程运行。</p> <p>鸿蒙内核每个进程内的线程独立运行、独立调度，当前进程内线程的调度不受其它进程内线程的影响。</p> <p>鸿蒙内核中的线程采用抢占式调度机制，同时支持时间片轮转调度和FIFO调度方式。</p> <p>鸿蒙内核的线程一共有32个优先级(0-31)，最高优先级为0，最低优先级为31。</p> <p>当前进程内高优先级的线程可抢占当前进程内低优先级线程，当前进程内低优先级线程必须在当前进程内高优先级线程阻塞或结束后才能得到调度。</p> <p>线程状态说明：</p> <p>初始化（Init）：该线程正在被创建。</p> <p>就绪（Ready）：该线程在就绪列表中，等待CPU调度。</p> <p>运行（Running）：该线程正在运行。</p> <p>阻塞（Blocked）：该线程被阻塞挂起。Blocked状态包括：pend(因为锁、事件、信号量等阻塞)、suspend（主动pend）、delay(延时阻塞)、pendtime(因为锁、事件、信号量时间等超时等待)。</p> <p>退出（Exit）：该线程运行结束，等待父线程回收其控制块资源。</p> <p>图 1 线程状态迁移示意图<br> <img src="https://weharmonyos.oss-cn-hangzhou.aliyuncs.com/resources/5/state.png" alt=""><br>
注意官方文档说的是线程，没有提到task(任务)，但内核源码中却有大量 task代码，很少有线程(thread)代码 ，这是怎么回事？<br>
其实在鸿蒙内核中， task就是线程， 初学者完全可以这么理解，但二者还是有区别，否则干嘛要分两个词描述。<br>
会有什么区别？是管理上的区别，task是调度层面的概念，线程是进程层面的概念。 就像同一个人在不同的管理体系中会有不同的身份一样，一个男人既可以是 孩子，爸爸，丈夫，或者程序员，视角不同功能也会不同。</p> <p>如何证明是一个东西，继续再往下看。</p> <h3 id="执行task命令"><strong>执行task命令</strong></h3> <p>看shell task 命令的执行结果:<br> <img src="https://weharmonyos.oss-cn-hangzhou.aliyuncs.com/resources/5/task.png" alt=""></p> <p>task命令 查出每个任务在生命周期内的运行情况，它运行的内存空间，优先级，时间片，入口执行函数，进程ID，状态等等信息，非常的复杂。这么复杂的信息就需要一个结构体来承载。而这个结构体就是 LosTaskCB(任务控制块)</p> <p>对应张大爷的故事：task就是一个用户的节目清单里的一个节目，用户总清单就是一个进程，所以上面会有很多的节目。</p> <h3 id="task长得什么样子"><strong>task长得什么样子</strong></h3> <p>说LosTaskCB之前先说下官方文档任务状态对应的 define，可以看出task和线程是一个东西。</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_INIT</span>         <span class="token expression"><span class="token number">0x0001U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_READY</span>        <span class="token expression"><span class="token number">0x0002U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_RUNNING</span>      <span class="token expression"><span class="token number">0x0004U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_SUSPEND</span>      <span class="token expression"><span class="token number">0x0008U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_PEND</span>         <span class="token expression"><span class="token number">0x0010U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_DELAY</span>        <span class="token expression"><span class="token number">0x0020U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_TIMEOUT</span>      <span class="token expression"><span class="token number">0x0040U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_PEND_TIME</span>    <span class="token expression"><span class="token number">0x0080U</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_TASK_STATUS_EXIT</span>         <span class="token expression"><span class="token number">0x0100U</span></span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>LosTaskCB长什么样？抱歉，它确实有点长，但还是要全部贴出全貌。</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    VOID            <span class="token operator">*</span>stackPointer<span class="token punctuation">;</span>      <span class="token comment">/**&lt; Task stack pointer | 内核栈指针位置(SP)  */</span>	
    UINT16          taskStatus<span class="token punctuation">;</span>         <span class="token comment">/**&lt; Task status | 各种状态标签，可以拥有多种标签，按位标识 */</span>
    UINT16          priority<span class="token punctuation">;</span>           <span class="token comment">/**&lt; Task priority | 任务优先级[0:31],默认是31级  */</span>
    UINT16          policy<span class="token punctuation">;</span>				<span class="token comment">///&lt; 任务的调度方式(三种 .. LOS_SCHED_RR   		  LOS_SCHED_FIFO .. )</span>
    UINT64          startTime<span class="token punctuation">;</span>          <span class="token comment">/**&lt; The start time of each phase of task | 任务开始时间  */</span>
    UINT64          irqStartTime<span class="token punctuation">;</span>       <span class="token comment">/**&lt; Interrupt start time | 任务中断开始时间  */</span> 
    UINT32          irqUsedTime<span class="token punctuation">;</span>        <span class="token comment">/**&lt; Interrupt consumption time | 任务中断消耗时间  */</span> 
    UINT32          initTimeSlice<span class="token punctuation">;</span>      <span class="token comment">/**&lt; Task init time slice | 任务初始的时间片  */</span> 
    INT32           timeSlice<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Task remaining time slice | 任务剩余时间片  */</span> 
    UINT32          waitTimes<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Task delay time, tick number | 设置任务调度延期时间  */</span> 
    SortLinkList    sortList<span class="token punctuation">;</span>           <span class="token comment">/**&lt; Task sortlink node | 跟CPU捆绑的任务排序链表节点,上面挂的是就绪队列的下一个阶段,进入CPU要执行的任务队列  */</span>	
    UINT32          stackSize<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Task stack size | 内核态栈大小,内存来自内核空间  */</span>		
    UINTPTR         topOfStack<span class="token punctuation">;</span>         <span class="token comment">/**&lt; Task stack top | 内核态栈顶 bottom = top + size */</span>		
    UINT32          taskID<span class="token punctuation">;</span>             <span class="token comment">/**&lt; Task ID | 任务ID，任务池本质是一个大数组，ID就是数组的索引，默认 &lt; 128 */</span>				
    TSK_ENTRY_FUNC  taskEntry<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Task entrance function | 任务执行入口地址 */</span>	
    VOID            <span class="token operator">*</span>joinRetval<span class="token punctuation">;</span>        <span class="token comment">/**&lt; pthread adaption | 用来存储join线程的入口地址 */</span>	
    VOID            <span class="token operator">*</span>taskMux<span class="token punctuation">;</span>           <span class="token comment">/**&lt; Task-held mutex | task在等哪把锁 */</span>		
    VOID            <span class="token operator">*</span>taskEvent<span class="token punctuation">;</span>         <span class="token comment">/**&lt; Task-held event | task在等哪个事件 */</span>		
    UINTPTR         args<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>            <span class="token comment">/**&lt; Parameter, of which the maximum number is 4 | 入口函数的参数 例如 main (int argc,char *argv[]) */</span>	
    CHAR            taskName<span class="token punctuation">[</span>OS_TCB_NAME_LEN<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">/**&lt; Task name | 任务的名称 */</span>	
    LOS_DL_LIST     pendList<span class="token punctuation">;</span>           <span class="token comment">/**&lt; Task pend node | 如果任务阻塞时就通过它挂到各种阻塞情况的链表上,比如OsTaskWait时 */</span>		
    LOS_DL_LIST     threadList<span class="token punctuation">;</span>         <span class="token comment">/**&lt; thread list | 挂到所属进程的线程链表上 */</span>
    UINT32          eventMask<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Event mask | 任务对哪些事件进行屏蔽 */</span>
    UINT32          eventMode<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Event mode | 事件三种模式(LOS_WAITMODE_AND,LOS_WAITMODE_OR,LOS_WAITMODE_CLR) */</span>	
    UINT32          priBitMap<span class="token punctuation">;</span>          <span class="token comment">/**&lt; BitMap for recording the change of task priority,the priority can not be greater than 31 
    										| 任务在执行过程中优先级会经常变化，这个变量用来记录所有曾经变化过的优先级，例如 ..01001011 曾经有过 0,1,3,6 优先级 */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_KERNEL_CPUP</span></span>
    OsCpupBase      taskCpup<span class="token punctuation">;</span>           <span class="token comment">/**&lt; task cpu usage | CPU 使用统计 */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
    INT32           errorNo<span class="token punctuation">;</span>            <span class="token comment">/**&lt; Error Num | 错误序号 */</span>
    UINT32          signal<span class="token punctuation">;</span>             <span class="token comment">/**&lt; Task signal | 任务信号类型,(SIGNAL_NONE,SIGNAL_KILL,SIGNAL_SUSPEND,SIGNAL_AFFI) */</span>
    sig_cb          sig<span class="token punctuation">;</span>				<span class="token comment">///&lt; 信号控制块，用于异步通信,类似于 linux singal模块</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_KERNEL_SMP</span></span>
    UINT16          currCpu<span class="token punctuation">;</span>            <span class="token comment">/**&lt; CPU core number of this task is running on | 正在运行此任务的CPU内核号 */</span>
    UINT16          lastCpu<span class="token punctuation">;</span>            <span class="token comment">/**&lt; CPU core number of this task is running on last time | 上次运行此任务的CPU内核号 */</span>
    UINT16          cpuAffiMask<span class="token punctuation">;</span>        <span class="token comment">/**&lt; CPU affinity mask, support up to 16 cores | CPU亲和力掩码，最多支持16核，亲和力很重要，多核情况下尽量一个任务在一个CPU核上运行，提高效率 */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_KERNEL_SMP_TASK_SYNC	</span><span class="token comment">//多核情况下的任务同步开关,采用信号量实现</span></span>
    UINT32          syncSignal<span class="token punctuation">;</span>         <span class="token comment">/**&lt; Synchronization for signal handling | 用于CPU之间同步信号量 */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_KERNEL_SMP_LOCKDEP </span><span class="token comment">//SMP死锁检测开关</span></span>
    LockDep         lockDep<span class="token punctuation">;</span>	<span class="token comment">///&lt; 死锁依赖检测</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_SCHED_DEBUG </span><span class="token comment">//调试调度开关</span></span>
    SchedStat       schedStat<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Schedule statistics | 调度统计 */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
    UINTPTR         userArea<span class="token punctuation">;</span>			<span class="token comment">///&lt; 用户空间的堆区开始位置</span>
    UINTPTR         userMapBase<span class="token punctuation">;</span>		<span class="token comment">///&lt; 用户空间的栈顶位置,内存来自用户空间,和topOfStack有本质的区别.</span>
    UINT32          userMapSize<span class="token punctuation">;</span>        <span class="token comment">/**&lt; user thread stack size ,real size : userMapSize + USER_STACK_MIN_SIZE | 用户栈大小 */</span>
    UINT32          processID<span class="token punctuation">;</span>          <span class="token comment">/**&lt; Which belong process | 所属进程ID */</span>
    FutexNode       futex<span class="token punctuation">;</span>				<span class="token comment">///&lt; 实现快锁功能</span>
    LOS_DL_LIST     joinList<span class="token punctuation">;</span>           <span class="token comment">/**&lt; join list | 联结链表,允许任务之间相互释放彼此 */</span>
    LOS_DL_LIST     lockList<span class="token punctuation">;</span>           <span class="token comment">/**&lt; Hold the lock list | 该链表上挂的都是已持有的锁 */</span>
    UINTPTR         waitID<span class="token punctuation">;</span>             <span class="token comment">/**&lt; Wait for the PID or GID of the child process | 等待子进程的PID或GID */</span>
    UINT16          waitFlag<span class="token punctuation">;</span>           <span class="token comment">/**&lt; The type of child process that is waiting, belonging to a group or parent,
                                             a specific child process, or any child process | 等待的子进程以什么样的方式结束(OS_TASK_WAIT_PROCESS | OS_TASK_WAIT_GID | ..) */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_KERNEL_LITEIPC </span><span class="token comment">//轻量级进程间通信开关</span></span>
    IpcTaskInfo     <span class="token operator">*</span>ipcTaskInfo<span class="token punctuation">;</span>	<span class="token comment">///&lt; 任务间通讯信息结构体</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_KERNEL_PERF</span></span>
    UINTPTR         pc<span class="token punctuation">;</span>	<span class="token comment">///&lt; pc寄存器</span>
    UINTPTR         fp<span class="token punctuation">;</span> <span class="token comment">///&lt; fp寄存器</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token punctuation">}</span> LosTaskCB<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br></div></div><p>结构体LosTaskCB内容很多，各代表什么含义？<br>
LosTaskCB相当于任务在内核中的身份证，它反映出每个任务在生命周期内的运行情况。既然是周期就会有状态，要运行就需要内存空间，就需要被内核算法调度，被选中CPU就去执行代码段指令，CPU要执行就需要告诉它从哪里开始执行，因为是多线程，但只有一个CPU就需要不断的切换任务，那执行会被中断，也需要再恢复后继续执行，又如何保证恢复的任务执行不会出错，这些问题都需要说明白。</p> <h3 id="task怎么管理"><strong>Task怎么管理</strong></h3> <h3 id="什么是任务池"><strong>什么是任务池？</strong></h3> <p>前面已经说了任务是内核调度层面的概念，调度算法保证了task有序的执行，调度机制详见其他姊妹篇的介绍。<br>
如此多的任务怎么管理和执行？管理靠任务池和就绪队列，执行靠调度算法。<br>
代码如下（OsTaskInit）：</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code>LITE_OS_SEC_TEXT_INIT UINT32 <span class="token function">OsTaskInit</span><span class="token punctuation">(</span>VOID<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    UINT32 index<span class="token punctuation">;</span>
    UINT32 ret<span class="token punctuation">;</span>
    UINT32 size<span class="token punctuation">;</span>

    g_taskMaxNum <span class="token operator">=</span> LOSCFG_BASE_CORE_TSK_LIMIT<span class="token punctuation">;</span><span class="token comment">//任务池中最多默认128个，可谓铁打的任务池流水的线程</span>
    size <span class="token operator">=</span> <span class="token punctuation">(</span>g_taskMaxNum <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>LosTaskCB<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//计算需分配内存总大小</span>
    <span class="token comment">/*
     * This memory is resident memory and is used to save the system resources
     * of task control block and will not be freed。
     */</span>
    g_taskCBArray <span class="token operator">=</span> <span class="token punctuation">(</span>LosTaskCB <span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">LOS_MemAlloc</span><span class="token punctuation">(</span>m_aucSysMem0， size<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//任务池 常驻内存，不被释放</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>g_taskCBArray <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> LOS_ERRNO_TSK_NO_MEMORY<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token punctuation">(</span>VOID<span class="token punctuation">)</span><span class="token function">memset_s</span><span class="token punctuation">(</span>g_taskCBArray， size， <span class="token number">0</span>， size<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">LOS_ListInit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_losFreeTask<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//空闲任务链表</span>
    <span class="token function">LOS_ListInit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_taskRecyleList<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//需回收任务链表</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> g_taskMaxNum<span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        g_taskCBArray<span class="token punctuation">[</span>index<span class="token punctuation">]</span>。taskStatus <span class="token operator">=</span> OS_TASK_STATUS_UNUSED<span class="token punctuation">;</span>
        g_taskCBArray<span class="token punctuation">[</span>index<span class="token punctuation">]</span>。taskID <span class="token operator">=</span> index<span class="token punctuation">;</span><span class="token comment">//任务ID最大默认127</span>
        <span class="token function">LOS_ListTailInsert</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_losFreeTask， <span class="token operator">&amp;</span>g_taskCBArray<span class="token punctuation">[</span>index<span class="token punctuation">]</span>。pendList<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//都插入空闲任务列表 </span>
    <span class="token punctuation">}</span><span class="token comment">//注意:这里挂的是pendList节点，所以取TCB要通过 OS_TCB_FROM_PENDLIST 取。</span>

    ret <span class="token operator">=</span> <span class="token function">OsPriQueueInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//创建32个任务优先级队列，即32个双向循环链表</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">!=</span> LOS_OK<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> LOS_ERRNO_TSK_NO_MEMORY<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/* init sortlink for each core */</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> LOSCFG_KERNEL_CORE_NUM<span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        ret <span class="token operator">=</span> <span class="token function">OsSortLinkInit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_percpu<span class="token punctuation">[</span>index<span class="token punctuation">]</span>。taskSortLink<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//每个CPU内核都有一个执行任务链表</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">!=</span> LOS_OK<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> LOS_ERRNO_TSK_NO_MEMORY<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> LOS_OK<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><p>g_taskCBArray 就是个任务池，默认创建128个任务，常驻内存，不被释放。<br>
g_losFreeTask是空闲任务链表，想创建任务时来这里申请一个空闲任务，用完了就回收掉，继续给后面的申请使用。<br>
g_taskRecyleList是回收任务链表，专用来回收exit 任务，任务所占资源被确认归还后被彻底删除，就像员工离职一样，得有个离职队列和流程，要归还电脑，邮箱，有没有借钱要还的 等操作。</p> <p>对应张大爷的故事：用户要来场馆领取表格填节目单，场馆只准备了128张表格，领完就没有了，但是节目表演完了会回收表格，这样多了一张表格就可以给其他人领取了，这128张表格对应鸿蒙内核这就是任务池，简单吧。</p> <h3 id="就绪队列是怎么回事"><strong>就绪队列是怎么回事</strong></h3> <p>CPU执行速度是很快的，鸿蒙内核默认一个时间片是 10ms， 资源有限，需要在众多任务中来回的切换，所以绝不能让CPU等待任务，CPU就像公司最大的领导，下面很多的部门等领导来审批，吃饭。只有大家等领导，哪有领导等你们的道理，所以工作要提前准备好，每个部门的优先级又不一样，所以每个部门都要有个任务队列，里面放的是领导能直接处理的任务，没准备好的不要放进来，因为这是给CPU提前准备好的粮食！<br>
这就是就绪队列的原理，一共有32个就绪队列，进程和线程都有，因为线程的优先级是默认32个， 每个队列中放同等优先级的task。<br>
还是看源码吧</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OS_PRIORITY_QUEUE_NUM</span> <span class="token expression"><span class="token number">32</span></span></span>
LITE_OS_SEC_BSS LOS_DL_LIST <span class="token operator">*</span>g_priQueueList <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span><span class="token comment">//队列链表</span>
LITE_OS_SEC_BSS UINT32 g_priQueueBitmap<span class="token punctuation">;</span><span class="token comment">//队列位图 UINT32每位代表一个优先级，共32个优先级</span>
<span class="token comment">//内部队列初始化</span>
UINT32 <span class="token function">OsPriQueueInit</span><span class="token punctuation">(</span>VOID<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    UINT32 priority<span class="token punctuation">;</span>

    <span class="token comment">/* system resident resource */</span><span class="token comment">//常驻内存</span>
    g_priQueueList <span class="token operator">=</span> <span class="token punctuation">(</span>LOS_DL_LIST <span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">LOS_MemAlloc</span><span class="token punctuation">(</span>m_aucSysMem0， <span class="token punctuation">(</span>OS_PRIORITY_QUEUE_NUM <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>LOS_DL_LIST<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//分配32个队列头节点</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>g_priQueueList <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> LOS_NOK<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span>priority <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> priority <span class="token operator">&lt;</span> OS_PRIORITY_QUEUE_NUM<span class="token punctuation">;</span> <span class="token operator">++</span>priority<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">LOS_ListInit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>g_priQueueList<span class="token punctuation">[</span>priority<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//队列初始化，前后指针指向自己</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> LOS_OK<span class="token punctuation">;</span>
<span class="token punctuation">}</span>


</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>注意看<code>g_priQueueList</code> 的内存分配，就是<code>32</code>个<code>LOS_DL_LIST</code>，还记得<code>LOS_DL_LIST</code>的妙用吗，不清楚去翻双向链表篇。</p> <p>对应张大爷的故事：就是门口那些排队的都是至少有一个节目单是符合表演标准的，资源都到位了，没有的连排队的资格都木有，就慢慢等吧。</p> <h3 id="任务栈是怎么回事"><strong>任务栈是怎么回事</strong></h3> <p>每个任务都是独立开的，任务之间也相互独立，之间通讯通过IPC，这里的“独立”指的是每个任务都有自己的运行环境 —— 栈空间，称为任务栈，栈空间里保存的信息包含局部变量、寄存器、函数参数、函数返回地址等等<br>
但系统中只有一个CPU，任务又是独立的，调度的本质就是CPU执行一个新task，老task在什么地方被中断谁也不清楚，是随机的。那如何保证老任务被再次调度选中时还能从上次被中断的地方继续玩下去呢？</p> <p>答案是：任务上下文，CPU内有一堆的寄存器，CPU运行本质的就是这些寄存器的值不断的变化，只要切换时把这些值保存起来，再还原回去就能保证task的连续执行，让用户毫无感知。鸿蒙内核给一个任务执行的时间是 20ms ，也就是说有多任务竞争的情况下，一秒钟内最多要来回切换50次。</p> <p>对应张大爷的故事：就是碰到节目没有表演完就必须打断的情况下，需要把当时的情况记录下来，比如小朋友在演躲猫猫的游戏，一半不演了，张三正在树上，李四正在厕所躲，都记录下来，下次再回来你们上次在哪就会哪呆着去，就位了继续表演。这样就接上了，观众就木有感觉了。<br>
任务上下文(TaskContext)是怎样的呢？还是直接看源码</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/* The size of this structure must be smaller than or equal to the size specified by OS_TSK_STACK_ALIGN (16 bytes). */</span>
<span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token punctuation">{</span> <span class="token comment">//参考OsTaskSchedule来理解</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression"><span class="token operator">!</span><span class="token function">defined</span><span class="token punctuation">(</span>LOSCFG_ARCH_FPU_DISABLE<span class="token punctuation">)</span> </span><span class="token comment">//支持浮点运算</span></span>
    UINT64 D<span class="token punctuation">[</span>FP_REGS_NUM<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">/* D0-D31 */</span>
    UINT32 regFPSCR<span class="token punctuation">;</span>       <span class="token comment">/* FPSCR */</span>
    UINT32 regFPEXC<span class="token punctuation">;</span>       <span class="token comment">/* FPEXC */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
    UINT32 R4<span class="token punctuation">;</span>
    UINT32 R5<span class="token punctuation">;</span>
    UINT32 R6<span class="token punctuation">;</span>
    UINT32 R7<span class="token punctuation">;</span>
    UINT32 R8<span class="token punctuation">;</span>
    UINT32 R9<span class="token punctuation">;</span>
    UINT32 R10<span class="token punctuation">;</span>
    UINT32 R11<span class="token punctuation">;</span>

    <span class="token comment">/* It has the same structure as IrqContext */</span>
    UINT32 reserved2<span class="token punctuation">;</span> <span class="token comment">/**&lt; Multiplexing registers, used in interrupts and system calls but with different meanings */</span>
    UINT32 reserved1<span class="token punctuation">;</span> <span class="token comment">/**&lt; Multiplexing registers, used in interrupts and system calls but with different meanings */</span>
    UINT32 USP<span class="token punctuation">;</span>       <span class="token comment">/**&lt; User mode sp register */</span>
    UINT32 ULR<span class="token punctuation">;</span>       <span class="token comment">/**&lt; User mode lr register */</span>
    UINT32 R0<span class="token punctuation">;</span>
    UINT32 R1<span class="token punctuation">;</span>
    UINT32 R2<span class="token punctuation">;</span>
    UINT32 R3<span class="token punctuation">;</span>
    UINT32 R12<span class="token punctuation">;</span>
    UINT32 LR<span class="token punctuation">;</span>
    UINT32 PC<span class="token punctuation">;</span>
    UINT32 regCPSR<span class="token punctuation">;</span>
<span class="token punctuation">}</span> TaskContext<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p>发现基本都是<code>CPU</code>寄存器的恢复现场值， 具体各寄存器有什么作用大家可以去网上详查，后续也有专门的文章来介绍。这里说其中的三个寄存器 <code>SP</code>， <code>LR</code>， <code>PC</code></p> <p><code>LR</code><br>
用途有二，一是保存子程序返回地址，当调用<code>BL</code>、<code>BX</code>、<code>BLX</code>等跳转指令时会自动保存返回地址到<code>LR</code>；二是保存异常发生的异常返回地址。</p> <p><code>PC（Program Counter）</code><br>
为程序计数器，用于保存程序的执行地址，在ARM的三级流水线架构中，程序流水线包括取址、译码和执行三个阶段，<code>PC</code>指向的是当前取址的程序地址，所以<code>32</code>位<code>ARM</code>中，译码地址（正在解析还未执行的程序）为<code>PC-4</code>，执行地址（当前正在执行的程序地址）为<code>PC-8</code>， 当突然发生中断的时候，保存的是<code>PC</code>的地址。</p> <p><code>SP</code><br>
每一种异常模式都有其自己独立的<code>r13</code>，它通常指向异常模式所专用的堆栈，当ARM进入异常模式的时候，程序就可以把一般通用寄存器压入堆栈，返回时再出栈，保证了各种模式下程序的状态的完整性。</p> <h3 id="任务栈初始化"><strong>任务栈初始化</strong></h3> <p>任务栈的初始化就是任务上下文的初始化，因为任务没开始执行，里面除了上下文不会有其他内容，注意上下文存放的位置在栈的底部。初始状态下 <code>sp</code>就是指向的栈底， 栈顶内容永远是 <code>0xCCCCCCCC</code> &quot;烫烫烫烫&quot;，这几个字应该很熟悉吗？ 如果不是那几个字了，那说明栈溢出了， 后续篇会详细说明这块，大家也可以自行去看代码，很有意思。</p> <h3 id="task函数集"><strong>Task函数集</strong></h3> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/// 内核态任务运行栈初始化</span>
LITE_OS_SEC_TEXT_INIT VOID <span class="token operator">*</span><span class="token function">OsTaskStackInit</span><span class="token punctuation">(</span>UINT32 taskID<span class="token punctuation">,</span> UINT32 stackSize<span class="token punctuation">,</span> VOID <span class="token operator">*</span>topStack<span class="token punctuation">,</span> BOOL initFlag<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>initFlag <span class="token operator">==</span> TRUE<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">OsStackInit</span><span class="token punctuation">(</span>topStack<span class="token punctuation">,</span> stackSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    TaskContext <span class="token operator">*</span>taskContext <span class="token operator">=</span> <span class="token punctuation">(</span>TaskContext <span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>UINTPTR<span class="token punctuation">)</span>topStack <span class="token operator">+</span> stackSize<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>TaskContext<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//上下文存放在栈的底部</span>
    <span class="token comment">/* initialize the task context */</span> <span class="token comment">//初始化任务上下文</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_GDB</span></span>
    taskContext<span class="token operator">-&gt;</span>PC <span class="token operator">=</span> <span class="token punctuation">(</span>UINTPTR<span class="token punctuation">)</span>OsTaskEntrySetupLoopFrame<span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
    taskContext<span class="token operator">-&gt;</span>PC <span class="token operator">=</span> <span class="token punctuation">(</span>UINTPTR<span class="token punctuation">)</span>OsTaskEntry<span class="token punctuation">;</span><span class="token comment">//内核态任务有统一的入口地址.</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
    taskContext<span class="token operator">-&gt;</span>LR <span class="token operator">=</span> <span class="token punctuation">(</span>UINTPTR<span class="token punctuation">)</span>OsTaskExit<span class="token punctuation">;</span>  <span class="token comment">/* LR should be kept, to distinguish it's THUMB or ARM instruction */</span>
    taskContext<span class="token operator">-&gt;</span>R0 <span class="token operator">=</span> taskID<span class="token punctuation">;</span>               <span class="token comment">/* R0 */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">LOSCFG_THUMB</span></span>
    taskContext<span class="token operator">-&gt;</span>regCPSR <span class="token operator">=</span> PSR_MODE_SVC_THUMB<span class="token punctuation">;</span> <span class="token comment">/* CPSR (Enable IRQ and FIQ interrupts, THUMNB-mode) */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span> <span class="token comment">//用于设置CPSR寄存器</span></span>
    taskContext<span class="token operator">-&gt;</span>regCPSR <span class="token operator">=</span> PSR_MODE_SVC_ARM<span class="token punctuation">;</span>   <span class="token comment">/* CPSR (Enable IRQ and FIQ interrupts, ARM-mode) */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression"><span class="token operator">!</span><span class="token function">defined</span><span class="token punctuation">(</span>LOSCFG_ARCH_FPU_DISABLE<span class="token punctuation">)</span></span></span>
    <span class="token comment">/* 0xAAA0000000000000LL : float reg initialed magic word */</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>UINT32 index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> FP_REGS_NUM<span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        taskContext<span class="token operator">-&gt;</span>D<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0xAAA0000000000000LL</span> <span class="token operator">+</span> index<span class="token punctuation">;</span> <span class="token comment">/* D0 - D31 */</span>
    <span class="token punctuation">}</span>
    taskContext<span class="token operator">-&gt;</span>regFPSCR <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    taskContext<span class="token operator">-&gt;</span>regFPEXC <span class="token operator">=</span> FP_EN<span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>VOID <span class="token operator">*</span><span class="token punctuation">)</span>taskContext<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h3 id="使用场景和功能"><strong>使用场景和功能</strong></h3> <p>任务创建后，内核可以执行锁任务调度，解锁任务调度，挂起，恢复，延时等操作，同时也可以设置任务优先级，获取任务优先级。任务结束的时候，则进行当前任务自删除操作。<br>
Huawei LiteOS 系统中的任务管理模块为用户提供下面几种功能。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>接口名 | 描述 
LOS_TaskCreateOnly | 创建任务，并使该任务进入suspend状态，并不调度。 
LOS_TaskCreate | 创建任务，并使该任务进入ready状态，并调度。 
LOS_TaskDelete | 删除指定的任务。 
LOS_TaskResume | 恢复挂起的任务。 
LOS_TaskSuspend | 挂起指定的任务。 
LOS_TaskDelay | 任务延时等待。
LOS_TaskYield | 显式放权，调整指定优先级的任务调度顺序。 
LOS_TaskLock | 锁任务调度。
LOS_TaskUnlock | 解锁任务调度。
LOS_CurTaskPriSet | 设置当前任务的优先级。
LOS_TaskPriSet | 设置指定任务的优先级。
LOS_TaskPriGet | 获取指定任务的优先级。
LOS_CurTaskIDGet | 获取当前任务的ID。
LOS_TaskInfoGet | 设置指定任务的优先级。
LOS_TaskPriGet | 获取指定任务的信息。
LOS_TaskStatusGet | 获取指定任务的状态。
LOS_TaskNameGet | 获取指定任务的名称。
LOS_TaskInfoMonitor | 监控所有任务，获取所有任务的信息。
LOS_NextTaskIDGet | 获取即将被调度的任务的ID。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="创建任务的过程"><strong>创建任务的过程</strong></h3> <p>创建任务之前先了解另一个结构体 tagTskInitParam</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">tagTskInitParam</span> <span class="token punctuation">{</span><span class="token comment">//Task的初始化参数</span>
    TSK_ENTRY_FUNC  pfnTaskEntry<span class="token punctuation">;</span>  <span class="token comment">/**&lt; Task entrance function */</span> <span class="token comment">//任务的入口函数</span>
    UINT16          usTaskPrio<span class="token punctuation">;</span>    <span class="token comment">/**&lt; Task priority */</span> <span class="token comment">//任务优先级</span>
    UINT16          policy<span class="token punctuation">;</span>        <span class="token comment">/**&lt; Task policy */</span>  <span class="token comment">//任务调度方式</span>
    UINTPTR         auwArgs<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>    <span class="token comment">/**&lt; Task parameters， of which the maximum number is four */</span> <span class="token comment">//入口函数的参数，最多四个</span>
    UINT32          uwStackSize<span class="token punctuation">;</span>   <span class="token comment">/**&lt; Task stack size */</span> <span class="token comment">//任务栈大小</span>
    CHAR            <span class="token operator">*</span>pcName<span class="token punctuation">;</span>       <span class="token comment">/**&lt; Task name */</span>  <span class="token comment">//任务名称</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression"><span class="token punctuation">(</span>LOSCFG_KERNEL_SMP <span class="token operator">==</span> YES<span class="token punctuation">)</span></span></span>
    UINT16          usCpuAffiMask<span class="token punctuation">;</span> <span class="token comment">/**&lt; Task cpu affinity mask         */</span> <span class="token comment">//任务cpu亲和力掩码</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
    UINT32          uwResved<span class="token punctuation">;</span>      <span class="token comment">/**&lt; It is automatically deleted if set to LOS_TASK_STATUS_DETACHED。
                                        It is unable to be deleted if set to 0。 */</span> <span class="token comment">//如果设置为LOS_TASK_STATUS_DETACHED，则自动删除。如果设置为0，则无法删除</span>
    UINT16          consoleID<span class="token punctuation">;</span>     <span class="token comment">/**&lt; The console id of task belongs  */</span> <span class="token comment">//任务的控制台id所属</span>
    UINT32          processID<span class="token punctuation">;</span> <span class="token comment">//进程ID</span>
    UserTaskParam   userParam<span class="token punctuation">;</span> <span class="token comment">//在用户态运行时栈参数</span>
<span class="token punctuation">}</span> TSK_INIT_PARAM_S<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>这些初始化参数是外露的任务初始参数，<code>pfnTaskEntry 对java来说就是你new进程的run()，</code>需要上层使用者提供。
看个例子吧:shell中敲 ping 命令看下它创建的过程</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token class-name">u32_t</span> <span class="token function">osShellPing</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc， <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>argv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
   
    <span class="token keyword">int</span> ret<span class="token punctuation">;</span>
    <span class="token class-name">u32_t</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token class-name">u32_t</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> count_set <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token class-name">u32_t</span> interval <span class="token operator">=</span> <span class="token number">1000</span><span class="token punctuation">;</span> <span class="token comment">/* default ping interval */</span>
    <span class="token class-name">u32_t</span> data_len <span class="token operator">=</span> <span class="token number">48</span><span class="token punctuation">;</span> <span class="token comment">/* default data length */</span>
    <span class="token class-name">ip4_addr_t</span> dst_ipaddr<span class="token punctuation">;</span>
    TSK_INIT_PARAM_S stPingTask<span class="token punctuation">;</span>
    <span class="token comment">// 。。。省去一些中间代码</span>
    <span class="token comment">/* start one task if ping forever or ping count greater than 60 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>count <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> count <span class="token operator">&gt;</span> LWIP_SHELL_CMD_PING_RETRY_TIMES<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ping_taskid <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
            <span class="token function">PRINTK</span><span class="token punctuation">(</span><span class="token string">&quot;Ping task already running and only support one now\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> LOS_NOK<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        stPingTask<span class="token punctuation">.</span>pfnTaskEntry <span class="token operator">=</span> <span class="token punctuation">(</span>TSK_ENTRY_FUNC<span class="token punctuation">)</span>ping_cmd<span class="token punctuation">;</span><span class="token comment">//线程的执行函数</span>
        stPingTask<span class="token punctuation">.</span>uwStackSize <span class="token operator">=</span> LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE<span class="token punctuation">;</span><span class="token comment">//0x4000 = 16K </span>
        stPingTask<span class="token punctuation">.</span>pcName <span class="token operator">=</span> <span class="token string">&quot;ping_task&quot;</span><span class="token punctuation">;</span>
        stPingTask<span class="token punctuation">.</span>usTaskPrio <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">;</span> <span class="token comment">/* higher than shell 优先级高于10，属于内核态线程*/</span> 
        stPingTask<span class="token punctuation">.</span>uwResved <span class="token operator">=</span> LOS_TASK_STATUS_DETACHED<span class="token punctuation">;</span>
        stPingTask<span class="token punctuation">.</span>auwArgs<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> dst_ipaddr。addr<span class="token punctuation">;</span> <span class="token comment">/* network order */</span>
        stPingTask<span class="token punctuation">.</span>auwArgs<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> count<span class="token punctuation">;</span>
        stPingTask<span class="token punctuation">.</span>auwArgs<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> interval<span class="token punctuation">;</span>
        stPingTask<span class="token punctuation">.</span>auwArgs<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> data_len<span class="token punctuation">;</span>
        ret <span class="token operator">=</span> <span class="token function">LOS_TaskCreate</span><span class="token punctuation">(</span><span class="token punctuation">(</span>UINT32 <span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ping_taskid<span class="token punctuation">)</span>， <span class="token operator">&amp;</span>stPingTask<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
 <span class="token comment">// ...</span>
    <span class="token keyword">return</span> LOS_OK<span class="token punctuation">;</span>
ping_error<span class="token operator">:</span>
    <span class="token function">lwip_ping_usage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> LOS_NOK<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><p>发现<code>ping</code>的调度优先级是<code>8</code>，比<code>shell</code> 还高，那<code>shell</code>的是多少？答案是:看源码是 <code>9</code></p> <div class="language-c line-numbers-mode"><pre class="language-c"><code>LITE_OS_SEC_TEXT_MINOR UINT32 <span class="token function">ShellTaskInit</span><span class="token punctuation">(</span>ShellCB <span class="token operator">*</span>shellCB<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
   
    CHAR <span class="token operator">*</span>name <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
    TSK_INIT_PARAM_S initParam <span class="token operator">=</span> <span class="token punctuation">{</span>
   <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>shellCB<span class="token operator">-&gt;</span>consoleID <span class="token operator">==</span> CONSOLE_SERIAL<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
        name <span class="token operator">=</span> SERIAL_SHELL_TASK_NAME<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>shellCB<span class="token operator">-&gt;</span>consoleID <span class="token operator">==</span> CONSOLE_TELNET<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
        name <span class="token operator">=</span> TELNET_SHELL_TASK_NAME<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
   
        <span class="token keyword">return</span> LOS_NOK<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    initParam<span class="token punctuation">.</span>pfnTaskEntry <span class="token operator">=</span> <span class="token punctuation">(</span>TSK_ENTRY_FUNC<span class="token punctuation">)</span>ShellTask<span class="token punctuation">;</span>
    initParam<span class="token punctuation">.</span>usTaskPrio   <span class="token operator">=</span> <span class="token number">9</span><span class="token punctuation">;</span> <span class="token comment">/* 9:shell task priority */</span>
    initParam<span class="token punctuation">.</span>auwArgs<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>   <span class="token operator">=</span> <span class="token punctuation">(</span>UINTPTR<span class="token punctuation">)</span>shellCB<span class="token punctuation">;</span>
    initParam<span class="token punctuation">.</span>uwStackSize  <span class="token operator">=</span> <span class="token number">0x3000</span><span class="token punctuation">;</span>
    initParam<span class="token punctuation">.</span>pcName       <span class="token operator">=</span> name<span class="token punctuation">;</span>
    initParam<span class="token punctuation">.</span>uwResved     <span class="token operator">=</span> LOS_TASK_STATUS_DETACHED<span class="token punctuation">;</span>
    <span class="token punctuation">(</span>VOID<span class="token punctuation">)</span><span class="token function">LOS_EventInit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>shellCB<span class="token operator">-&gt;</span>shellEvent<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">LOS_TaskCreate</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>shellCB<span class="token operator">-&gt;</span>shellTaskHandle， <span class="token operator">&amp;</span>initParam<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>关于shell后续会详细介绍，请持续关注。<br>
前置条件了解清楚后，具体看任务是如何一步步创建的，如何和进程绑定，加入调度就绪队列，还是继续看源码</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//创建Task</span>
LITE_OS_SEC_TEXT_INIT UINT32 <span class="token function">LOS_TaskCreate</span><span class="token punctuation">(</span>UINT32 <span class="token operator">*</span>taskID， TSK_INIT_PARAM_S <span class="token operator">*</span>initParam<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    UINT32 ret<span class="token punctuation">;</span>
    UINT32 intSave<span class="token punctuation">;</span>
    LosTaskCB <span class="token operator">*</span>taskCB <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>initParam <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> LOS_ERRNO_TSK_PTR_NULL<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>OS_INT_ACTIVE<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> LOS_ERRNO_TSK_YIELD_IN_INT<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>initParam<span class="token operator">-&gt;</span>uwResved <span class="token operator">&amp;</span> OS_TASK_FLAG_IDLEFLAG<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//OS_TASK_FLAG_IDLEFLAG 是属于内核 idle进程专用的</span>
        initParam<span class="token operator">-&gt;</span>processID <span class="token operator">=</span> <span class="token function">OsGetIdleProcessID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获取空闲进程</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">OsProcessIsUserMode</span><span class="token punctuation">(</span><span class="token function">OsCurrProcessGet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//当前进程是否为用户模式</span>
        initParam<span class="token operator">-&gt;</span>processID <span class="token operator">=</span> <span class="token function">OsGetKernelInitProcessID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//不是就取&quot;Kernel&quot;进程</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        initParam<span class="token operator">-&gt;</span>processID <span class="token operator">=</span> <span class="token function">OsCurrProcessGet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span>processID<span class="token punctuation">;</span><span class="token comment">//获取当前进程 ID赋值</span>
    <span class="token punctuation">}</span>
    initParam<span class="token operator">-&gt;</span>uwResved <span class="token operator">&amp;=</span> <span class="token operator">~</span>OS_TASK_FLAG_IDLEFLAG<span class="token punctuation">;</span><span class="token comment">//不能是 OS_TASK_FLAG_IDLEFLAG</span>
    initParam<span class="token operator">-&gt;</span>uwResved <span class="token operator">&amp;=</span> <span class="token operator">~</span>OS_TASK_FLAG_PTHREAD_JOIN<span class="token punctuation">;</span><span class="token comment">//不能是 OS_TASK_FLAG_PTHREAD_JOIN</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>initParam<span class="token operator">-&gt;</span>uwResved <span class="token operator">&amp;</span> LOS_TASK_STATUS_DETACHED<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//是否设置了自动删除</span>
        initParam<span class="token operator">-&gt;</span>uwResved <span class="token operator">=</span> OS_TASK_FLAG_DETACHED<span class="token punctuation">;</span><span class="token comment">//自动删除，注意这里是 = ，也就是说只有 OS_TASK_FLAG_DETACHED 一个标签了</span>
    <span class="token punctuation">}</span>

    ret <span class="token operator">=</span> <span class="token function">LOS_TaskCreateOnly</span><span class="token punctuation">(</span>taskID， initParam<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//创建一个任务，这是任务创建的实体，前面都只是前期准备工作</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ret <span class="token operator">!=</span> LOS_OK<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> ret<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    taskCB <span class="token operator">=</span> <span class="token function">OS_TCB_FROM_TID</span><span class="token punctuation">(</span><span class="token operator">*</span>taskID<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//通过ID拿到task实体</span>

    <span class="token function">SCHEDULER_LOCK</span><span class="token punctuation">(</span>intSave<span class="token punctuation">)</span><span class="token punctuation">;</span>
    taskCB<span class="token operator">-&gt;</span>taskStatus <span class="token operator">&amp;=</span> <span class="token operator">~</span>OS_TASK_STATUS_INIT<span class="token punctuation">;</span><span class="token comment">//任务不再是初始化</span>
    <span class="token function">OS_TASK_SCHED_QUEUE_ENQUEUE</span><span class="token punctuation">(</span>taskCB， <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//进入调度就绪队列，新任务是直接进入就绪队列的</span>
    <span class="token function">SCHEDULER_UNLOCK</span><span class="token punctuation">(</span>intSave<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">/* in case created task not running on this core，
       schedule or not depends on other schedulers status。 */</span>
    <span class="token function">LOS_MpSchedule</span><span class="token punctuation">(</span>OS_MP_CPU_ALL<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//如果创建的任务没有在这个核心上运行，是否调度取决于其他调度程序的状态。</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>OS_SCHEDULER_ACTIVE<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//当前CPU核处于可调度状态</span>
        <span class="token function">LOS_Schedule</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//发起调度</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> LOS_OK<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br></div></div><p>对应张大爷的故事：就是节目单要怎么填，按格式来，从哪里开始演，要多大的空间，王场馆好协调好现场的环境。这里注意 在同一个节目单只要节目没演完，王场馆申请场地的空间就不能给别人用，这个场地空间对应的就是鸿蒙任务的栈空间，除非整个节目单都完了，就回收了。把整个场地干干净净的留给下一个人的节目单来表演。</p> <p>至此的创建已经完成，已各就各位，源码最后还申请了一次<code>LOS_Schedule()</code>;因为鸿蒙的调度方式是抢占式的，如何本次<code>task</code>的任务优先级高于其他就绪队列，那么接下来要执行的任务就是它了！</p> <h3 id="百文说内核-抓住主脉络">百文说内核 | 抓住主脉络</h3> <ul><li>百文相当于摸出内核的肌肉和器官系统，让人开始丰满有立体感，因是直接从注释源码起步，在加注释过程中，每每有心得处就整理,慢慢形成了以下文章。内容立足源码，常以生活场景打比方尽可能多的将内核知识点置入某种场景，具有画面感，容易理解记忆。说别人能听得懂的话很重要! 百篇博客绝不是百度教条式的在说一堆诘屈聱牙的概念，那没什么意思。更希望让内核变得栩栩如生，倍感亲切。</li> <li>与代码需不断<code>debug</code>一样，文章内容会存在不少错漏之处，请多包涵，但会反复修正，持续更新，<code>v**.xx</code> 代表文章序号和修改的次数，精雕细琢，言简意赅，力求打造精品内容。</li> <li>百文在 &lt; 鸿蒙研究站 | 开源中国 | 博客园 | 51cto | csdn | 知乎 | 掘金 &gt; 站点发布，百篇博客系列目录如下。</li> <li><img src="https://weharmonyos.oss-cn-hangzhou.aliyuncs.com/resources/common/cate.png" alt=""></li></ul> <p>按功能模块:</p> <table><thead><tr><th style="text-align:center;">基础知识</th> <th style="text-align:center;">进程管理</th> <th style="text-align:center;">任务管理</th> <th style="text-align:center;">内存管理</th></tr></thead> <tbody><tr><td style="text-align:center;"><a href="http://weharmonyos.com/blog/01.html" target="_blank" rel="noopener noreferrer">双向链表</a><br><a href="http://weharmonyos.com/blog/02.html" target="_blank" rel="noopener noreferrer">内核概念</a><br><a href="http://weharmonyos.com/blog/03.html" target="_blank" rel="noopener noreferrer">源码结构</a><br><a href="http://weharmonyos.com/blog/04.html" target="_blank" rel="noopener noreferrer">地址空间</a><br><a href="http://weharmonyos.com/blog/05.html" target="_blank" rel="noopener noreferrer">计时单位</a><br><a href="http://weharmonyos.com/blog/06.html" target="_blank" rel="noopener noreferrer">优雅的宏</a><br><a href="http://weharmonyos.com/blog/07.html" target="_blank" rel="noopener noreferrer">钩子框架</a><br><a href="http://weharmonyos.com/blog/08.html" target="_blank" rel="noopener noreferrer">位图管理</a><br><a href="http://weharmonyos.com/blog/09.html" target="_blank" rel="noopener noreferrer">POSIX</a><br><a href="http://weharmonyos.com/blog/10.html" target="_blank" rel="noopener noreferrer">main函数</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/11.html" target="_blank" rel="noopener noreferrer">调度故事</a><br><a href="http://weharmonyos.com/blog/12.html" target="_blank" rel="noopener noreferrer">进程控制块</a><br><a href="http://weharmonyos.com/blog/13.html" target="_blank" rel="noopener noreferrer">进程空间</a><br><a href="http://weharmonyos.com/blog/14.html" target="_blank" rel="noopener noreferrer">线性区</a><br><a href="http://weharmonyos.com/blog/15.html" target="_blank" rel="noopener noreferrer">红黑树</a><br><a href="http://weharmonyos.com/blog/16.html" target="_blank" rel="noopener noreferrer">进程管理</a><br><a href="http://weharmonyos.com/blog/17.html" target="_blank" rel="noopener noreferrer">Fork进程</a><br><a href="http://weharmonyos.com/blog/18.html" target="_blank" rel="noopener noreferrer">进程回收</a><br><a href="http://weharmonyos.com/blog/19.html" target="_blank" rel="noopener noreferrer">Shell编辑</a><br><a href="http://weharmonyos.com/blog/20.html" target="_blank" rel="noopener noreferrer">Shell解析</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/21.html" target="_blank" rel="noopener noreferrer">任务控制块</a><br><a href="http://weharmonyos.com/blog/22.html" target="_blank" rel="noopener noreferrer">并发并行</a><br><a href="http://weharmonyos.com/blog/23.html" target="_blank" rel="noopener noreferrer">就绪队列</a><br><a href="http://weharmonyos.com/blog/24.html" target="_blank" rel="noopener noreferrer">调度机制</a><br><a href="http://weharmonyos.com/blog/25.html" target="_blank" rel="noopener noreferrer">任务管理</a><br><a href="http://weharmonyos.com/blog/26.html" target="_blank" rel="noopener noreferrer">用栈方式</a><br><a href="http://weharmonyos.com/blog/27.html" target="_blank" rel="noopener noreferrer">软件定时器</a><br><a href="http://weharmonyos.com/blog/28.html" target="_blank" rel="noopener noreferrer">控制台</a><br><a href="http://weharmonyos.com/blog/29.html" target="_blank" rel="noopener noreferrer">远程登录</a><br><a href="http://weharmonyos.com/blog/30.html" target="_blank" rel="noopener noreferrer">协议栈</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/31.html" target="_blank" rel="noopener noreferrer">内存规则</a><br><a href="http://weharmonyos.com/blog/32.html" target="_blank" rel="noopener noreferrer">物理内存</a><br><a href="http://weharmonyos.com/blog/33.html" target="_blank" rel="noopener noreferrer">内存概念</a><br><a href="http://weharmonyos.com/blog/34.html" target="_blank" rel="noopener noreferrer">虚实映射</a><br><a href="http://weharmonyos.com/blog/35.html" target="_blank" rel="noopener noreferrer">页表管理</a><br><a href="http://weharmonyos.com/blog/36.html" target="_blank" rel="noopener noreferrer">静态分配</a><br><a href="http://weharmonyos.com/blog/37.html" target="_blank" rel="noopener noreferrer">TLFS算法</a><br><a href="http://weharmonyos.com/blog/38.html" target="_blank" rel="noopener noreferrer">内存池管理</a><br><a href="http://weharmonyos.com/blog/39.html" target="_blank" rel="noopener noreferrer">原子操作</a><br><a href="http://weharmonyos.com/blog/40.html" target="_blank" rel="noopener noreferrer">圆整对齐</a><br></td></tr> <tr><td style="text-align:center;">通讯机制</td> <td style="text-align:center;">文件系统</td> <td style="text-align:center;">硬件架构</td> <td style="text-align:center;">内核汇编</td></tr> <tr><td style="text-align:center;"><a href="http://weharmonyos.com/blog/41.html" target="_blank" rel="noopener noreferrer">通讯总览</a><br><a href="http://weharmonyos.com/blog/42.html" target="_blank" rel="noopener noreferrer">自旋锁</a><br><a href="http://weharmonyos.com/blog/43.html" target="_blank" rel="noopener noreferrer">互斥锁</a><br><a href="http://weharmonyos.com/blog/44.html" target="_blank" rel="noopener noreferrer">快锁使用</a><br><a href="http://weharmonyos.com/blog/45.html" target="_blank" rel="noopener noreferrer">快锁实现</a><br><a href="http://weharmonyos.com/blog/46.html" target="_blank" rel="noopener noreferrer">读写锁</a><br><a href="http://weharmonyos.com/blog/47.html" target="_blank" rel="noopener noreferrer">信号量</a><br><a href="http://weharmonyos.com/blog/48.html" target="_blank" rel="noopener noreferrer">事件机制</a><br><a href="http://weharmonyos.com/blog/49.html" target="_blank" rel="noopener noreferrer">信号生产</a><br><a href="http://weharmonyos.com/blog/50.html" target="_blank" rel="noopener noreferrer">信号消费</a><br><a href="http://weharmonyos.com/blog/51.html" target="_blank" rel="noopener noreferrer">消息队列</a><br><a href="http://weharmonyos.com/blog/52.html" target="_blank" rel="noopener noreferrer">消息封装</a><br><a href="http://weharmonyos.com/blog/53.html" target="_blank" rel="noopener noreferrer">消息映射</a><br><a href="http://weharmonyos.com/blog/54.html" target="_blank" rel="noopener noreferrer">共享内存</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/55.html" target="_blank" rel="noopener noreferrer">文件概念</a><br><a href="http://weharmonyos.com/blog/56.html" target="_blank" rel="noopener noreferrer">文件故事</a><br><a href="http://weharmonyos.com/blog/57.html" target="_blank" rel="noopener noreferrer">索引节点</a><br><a href="http://weharmonyos.com/blog/58.html" target="_blank" rel="noopener noreferrer">VFS</a><br><a href="http://weharmonyos.com/blog/59.html" target="_blank" rel="noopener noreferrer">文件句柄</a><br><a href="http://weharmonyos.com/blog/60.html" target="_blank" rel="noopener noreferrer">根文件系统</a><br><a href="http://weharmonyos.com/blog/61.html" target="_blank" rel="noopener noreferrer">挂载机制</a><br><a href="http://weharmonyos.com/blog/62.html" target="_blank" rel="noopener noreferrer">管道文件</a><br><a href="http://weharmonyos.com/blog/63.html" target="_blank" rel="noopener noreferrer">文件映射</a><br><a href="http://weharmonyos.com/blog/64.html" target="_blank" rel="noopener noreferrer">写时拷贝</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/65.html" target="_blank" rel="noopener noreferrer">芯片模式</a><br><a href="http://weharmonyos.com/blog/66.html" target="_blank" rel="noopener noreferrer">ARM架构</a><br><a href="http://weharmonyos.com/blog/67.html" target="_blank" rel="noopener noreferrer">指令集</a><br><a href="http://weharmonyos.com/blog/68.html" target="_blank" rel="noopener noreferrer">协处理器</a><br><a href="http://weharmonyos.com/blog/69.html" target="_blank" rel="noopener noreferrer">工作模式</a><br><a href="http://weharmonyos.com/blog/70.html" target="_blank" rel="noopener noreferrer">寄存器</a><br><a href="http://weharmonyos.com/blog/71.html" target="_blank" rel="noopener noreferrer">多核管理</a><br><a href="http://weharmonyos.com/blog/72.html" target="_blank" rel="noopener noreferrer">中断概念</a><br><a href="http://weharmonyos.com/blog/73.html" target="_blank" rel="noopener noreferrer">中断管理</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/74.html" target="_blank" rel="noopener noreferrer">编码方式</a><br><a href="http://weharmonyos.com/blog/75.html" target="_blank" rel="noopener noreferrer">汇编基础</a><br><a href="http://weharmonyos.com/blog/76.html" target="_blank" rel="noopener noreferrer">汇编传参</a><br><a href="http://weharmonyos.com/blog/77.html" target="_blank" rel="noopener noreferrer">链接脚本</a><br><a href="http://weharmonyos.com/blog/78.html" target="_blank" rel="noopener noreferrer">内核启动</a><br><a href="http://weharmonyos.com/blog/79.html" target="_blank" rel="noopener noreferrer">进程切换</a><br><a href="http://weharmonyos.com/blog/80.html" target="_blank" rel="noopener noreferrer">任务切换</a><br><a href="http://weharmonyos.com/blog/81.html" target="_blank" rel="noopener noreferrer">中断切换</a><br><a href="http://weharmonyos.com/blog/82.html" target="_blank" rel="noopener noreferrer">异常接管</a><br><a href="http://weharmonyos.com/blog/83.html" target="_blank" rel="noopener noreferrer">缺页中断</a><br></td></tr> <tr><td style="text-align:center;">编译运行</td> <td style="text-align:center;">调测工具</td> <td style="text-align:center;"></td> <td style="text-align:center;"></td></tr> <tr><td style="text-align:center;"><a href="http://weharmonyos.com/blog/84.html" target="_blank" rel="noopener noreferrer">编译过程</a><br><a href="http://weharmonyos.com/blog/85.html" target="_blank" rel="noopener noreferrer">编译构建</a><br><a href="http://weharmonyos.com/blog/86.html" target="_blank" rel="noopener noreferrer">GN语法</a><br><a href="http://weharmonyos.com/blog/87.html" target="_blank" rel="noopener noreferrer">忍者无敌</a><br><a href="http://weharmonyos.com/blog/88.html" target="_blank" rel="noopener noreferrer">ELF格式</a><br><a href="http://weharmonyos.com/blog/89.html" target="_blank" rel="noopener noreferrer">ELF解析</a><br><a href="http://weharmonyos.com/blog/90.html" target="_blank" rel="noopener noreferrer">静态链接</a><br><a href="http://weharmonyos.com/blog/91.html" target="_blank" rel="noopener noreferrer">重定位</a><br><a href="http://weharmonyos.com/blog/92.html" target="_blank" rel="noopener noreferrer">动态链接</a><br><a href="http://weharmonyos.com/blog/93.html" target="_blank" rel="noopener noreferrer">进程映像</a><br><a href="http://weharmonyos.com/blog/94.html" target="_blank" rel="noopener noreferrer">应用启动</a><br><a href="http://weharmonyos.com/blog/95.html" target="_blank" rel="noopener noreferrer">系统调用</a><br><a href="http://weharmonyos.com/blog/96.html" target="_blank" rel="noopener noreferrer">VDSO</a><br></td> <td style="text-align:center;"><a href="http://weharmonyos.com/blog/97.html" target="_blank" rel="noopener noreferrer">模块监控</a><br><a href="http://weharmonyos.com/blog/98.html" target="_blank" rel="noopener noreferrer">日志跟踪</a><br><a href="http://weharmonyos.com/blog/99.html" target="_blank" rel="noopener noreferrer">系统安全</a><br><a href="http://weharmonyos.com/blog/100.html" target="_blank" rel="noopener noreferrer">测试用例</a><br></td> <td style="text-align:center;"></td> <td style="text-align:center;"></td></tr></tbody></table> <h3 id="百万注源码-处处扣细节">百万注源码 | 处处扣细节</h3> <ul><li><p>百万汉字注解内核目的是要看清楚其毛细血管，细胞结构，等于在拿放大镜看内核。内核并不神秘，带着问题去源码中找答案是很容易上瘾的，你会发现很多文章对一些问题的解读是错误的，或者说不深刻难以自圆其说，你会慢慢形成自己新的解读，而新的解读又会碰到新的问题，如此层层递进，滚滚向前，拿着放大镜根本不愿意放手。</p></li> <li><p><a href="https://gitee.com/weharmony/kernel_liteos_a_note" target="_blank" rel="noopener noreferrer">&lt; gitee</a> | <a href="https://github.com/kuangyufei/kernel_liteos_a_note" target="_blank" rel="noopener noreferrer">github</a> | <a href="https://weharmony.coding.net/public/harmony/kernel_liteos_a_note/git/files" target="_blank" rel="noopener noreferrer">coding</a> | <a href="https://gitcode.net/kuangyufei/kernel_liteos_a_note" target="_blank" rel="noopener noreferrer">gitcode &gt;</a> 四大码仓推送 | 同步官方源码。</p> <p><a href="https://gitee.com/weharmony/kernel_liteos_a_note" target="_blank" rel="noopener noreferrer"><img src="https://gitee.com/weharmony/kernel_liteos_a_note/widgets/widget_card.svg?colors=393222,ebdfc1,fffae5,d8ca9f,393222,a28b40" alt=""></a></p></li></ul> <h3 id="关注不迷路-代码即人生">关注不迷路 | 代码即人生</h3> <p><img src="https://weharmonyos.oss-cn-hangzhou.aliyuncs.com/resources/common/so1so.png" alt=""></p> <ul><li>互联网从业十五年，计算机硕士，技术副总裁</li> <li>关注我，持续更新四十年，即聊技术也聊人生</li> <li>交有趣靠谱的人；做难而正确的事</li> <li>不做作，不炒作，只唯真</li> <li>不唯上，不唯书，只唯实</li></ul> <p><a href="http://weharmonyos.com/donate.html" target="_blank" rel="noopener noreferrer"> &gt;&gt; 捐助名单</a></p> <p>据说喜欢 <strong>点赞 + 分享</strong> 的,后来都成了大神。😃</p></div> <!----> <div class="content__content-bottom"></div> <footer class="page-meta"><!----> <!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blog/24.html" class="prev"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="icon prev-icon"><path d="M906.783 588.79c-.02 8.499-6.882 15.36-15.38 15.37l-443.7-.01 75.704 191.682c2.52 6.42.482 13.763-5.038 17.91-5.52 4.168-13.138 4.147-18.616-.092L123.228 524.175a15.362 15.362 0 0 1-6-12.165c0-4.782 2.222-9.277 6-12.185L499.753 210.35a15.388 15.388 0 0 1 9.38-3.195c3.236 0 6.502 1.034 9.236 3.103 5.52 4.147 7.578 11.49 5.038 17.91L447.683 419.84l443.72-.01c8.498.01 15.36 6.881 15.36 15.36l.02 153.6z" fill="currentColor"></path></svg>
        调度机制
      </a></span> <span class="next"><a href="/blog/26.html">
        用栈方式
        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="icon next-icon"><path d="M906.772 512c0 4.772-2.211 9.267-5.99 12.175L524.257 813.66a15.37 15.37 0 0 1-18.616.092 15.368 15.368 0 0 1-5.038-17.91l75.714-191.672h-443.73c-8.488 0-15.36-6.881-15.36-15.36v-153.6c0-8.489 6.872-15.36 15.36-15.36h443.73l-75.714-191.682a15.358 15.358 0 0 1 5.048-17.91c5.51-4.158 13.128-4.137 18.606.092l376.525 289.485a15.323 15.323 0 0 1 5.99 12.165z" fill="currentColor"></path></svg></a></span></p></div> <!----> <!----> <div class="content__page-bottom"></div></main> <!----></div><div class="global-ui"><!----><!----><div id="pwa-install"><!----> <div id="install-modal-wrapper" style="display:none;"><div class="background"></div> <div class="install-modal"><div class="header"><button aria-label="Close" class="close-button"><svg width="23" height="22" xmlns="http://www.w3.org/2000/svg" class="icon close-icon"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.12.358a1.224 1.224 0 011.729 0l8.92 8.914L20.686.358a1.224 1.224 0 011.73 1.728L13.497 11l8.92 8.913a1.222 1.222 0 11-1.73 1.729l-8.919-8.913-8.92 8.913a1.224 1.224 0 01-1.729-1.729L10.04 11l-8.92-8.914a1.222 1.222 0 010-1.728z" fill="currentColor"></path></svg></button> <div class="logo"><!----> <div class="title"><h1></h1> <p class="desc">This app can be installed on your PC or mobile device.  This will allow this web app to look and behave like any other installed app.  You will find it in your app lists and be able to pin it to your home screen, start menus or task bars.  This installed web app will also be able to safely interact with other apps and your operating system. </p></div></div></div> <div class="content"><div class="highlight"><!----> <!----></div> <div class="description"><h3>Description</h3> <p></p></div></div> <div class="button-wrapper"><button class="install-button">
        Install <span></span></button> <button class="cancel-button">
        Cancel
      </button></div></div></div></div><div tabindex="-1" role="dialog" aria-hidden="true" class="pswp"><div class="pswp__bg"></div> <div class="pswp__scroll-wrap"><div class="pswp__container"><div class="pswp__item"></div> <div class="pswp__item"></div> <div class="pswp__item"></div></div> <div class="pswp__ui pswp__ui--hidden"><div class="pswp__top-bar"><div class="pswp__counter"></div> <button title="Close" aria-label="Close" class="pswp__button pswp__button--close"></button> <button title="Share" aria-label="Share" class="pswp__button pswp__button--share"></button> <button title="Switch to full screen" aria-label="Switch to full screen" class="pswp__button pswp__button--fs"></button> <button title="Zoom in/out" aria-label="Zoom in/out" class="pswp__button pswp__button--zoom"></button> <div class="pswp__preloader"><div class="pswp__preloader__icn"><div class="pswp__preloader__cut"><div class="pswp__preloader__donut"></div></div></div></div></div> <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap"><div class="pswp__share-tooltip"></div></div> <button title="Prev (Arrow Left)" aria-label="Prev (Arrow Left)" class="pswp__button pswp__button--arrow--left"></button> <button title="Next (Arrow Right)" aria-label="Next (Arrow Right)" class="pswp__button pswp__button--arrow--right"></button> <div class="pswp__caption"><div class="pswp__caption__center"></div></div></div></div></div></div></div>
    <script src="/assets/js/app.9a8b95d8.js" defer></script><script src="/assets/js/vendors~layout-Layout.c1e797d4.js" defer></script><script src="/assets/js/vendors~layout-Blog~layout-Layout~layout-NotFound.f75cf258.js" defer></script><script src="/assets/js/page--10bd3330.b5165172.js" defer></script>
  </body>
</html>
